// INSE6260-ATM Simulator Project

/**
 * View package contains ATMS GUI.
 */
package com.inse6260.atms.view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JPanel;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


import com.inse6260.atms.controller.ATMSController;

/**
 * @author dimitri.tiago
 */
public class Screen extends javax.swing.JFrame
{
    // GUI components
    private LogInPanel          logInPanel;
    private Keypad              keypadPanel;
    private ATMSMainMenu        atmsMainMenuPanel;
    private WithdrawalPanel     atmsWithdrawalPanel;
    private AccSelPanel         atmsAccSelPanel;
    private AnotherTransPanel   atmsAnotherTransPanel;
    private AmountInputPanel    atmsAmtInputPanel;
    private TgtAccSelPanel      atmsTgtAccSelPanel;
    private AccInfoMenuPanel    atmsAccInfoMenuPanel;
    private AccInformationPanel atmsAccInfoPanel;

    private JButton         cashDispenser;
    private JButton         envelopeSlot;
    private JPanel          southSlotDispenser;

    private JPanel          currentScreen;

    // controller
    private ATMSController atmsController;

    /**
     * Default constructor instantiates screen
     */
    public Screen(final ATMSController atmsController)
    {
        initComponents();

        // initialize fields
        this.atmsController = atmsController;
        logInPanel  = new LogInPanel();
        atmsMainMenuPanel = new ATMSMainMenu(atmsController);
        atmsWithdrawalPanel = new WithdrawalPanel(atmsController);
        atmsAccSelPanel = new AccSelPanel(atmsController);
        atmsAnotherTransPanel = new AnotherTransPanel(atmsController);
        atmsAmtInputPanel = new AmountInputPanel(atmsController);
        atmsTgtAccSelPanel = new TgtAccSelPanel(atmsController);
        atmsAccInfoMenuPanel = new AccInfoMenuPanel(atmsController);
        atmsAccInfoPanel = new AccInformationPanel(atmsController);

        currentScreen = null;

        // create south of screen
        southSlotDispenser = new JPanel(new GridLayout(2, 1, 2, 2));
        cashDispenser = new JButton("CASH DISPENSER");
        envelopeSlot  = new JButton("ENVELOPE SLOT");
        cashDispenser.setEnabled(false);
        cashDispenser.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                cashDispenser.setText("CASH DISPENSER");
                cashDispenser.setEnabled(false);
            }
        });
        envelopeSlot.setEnabled(false);
        envelopeSlot.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                envelopeSlot.setText("ENVELOPE SLOT");
                envelopeSlot.setEnabled(false);

                // request controller to deposit
                atmsController.requestDeposit();
            }
        });
        southSlotDispenser.add(cashDispenser);
        southSlotDispenser.add(envelopeSlot);

        // attach keypad to log in panel
        keypadPanel = new Keypad(atmsController);
        keypadPanel.setCurrentScreenPanel(logInPanel);

        // add components
        add(logInPanel, BorderLayout.CENTER);    // add panels

        JPanel southPanel = new JPanel();
        GridLayout grdLayout = new GridLayout(1,2,5,5);
        southPanel.setLayout(grdLayout);
        southPanel.add(southSlotDispenser);
        southPanel.add(keypadPanel);
        add(southPanel, BorderLayout.SOUTH);

        // dispplay screen
        setSize(500, 600);
        setVisible(true);
    }

    /**
     * Show/hide screen panel
     * @param visible determines screen should be shown or hidden.
     */
    public void showCurrentScreenPanel(boolean visible)
    {
       JPanel currentScreenPanel = keypadPanel.getCurrentSceenPanel();
       currentScreenPanel.setVisible(visible);
    }

    /**
     * Sets option panel as the active screen
     */
    public void setATMSMainManuPanel()
    {
        currentScreen = atmsMainMenuPanel;
        add(atmsMainMenuPanel, BorderLayout.CENTER);
        keypadPanel.setCurrentScreenPanel(atmsMainMenuPanel);
        currentScreen.setVisible(true);
    }

    /**
     * Sets option panel as the active screen
     */
    public void setWithdrawalPanel()
    {
        currentScreen = atmsWithdrawalPanel;
        add(atmsWithdrawalPanel, BorderLayout.CENTER);
        keypadPanel.setCurrentScreenPanel(atmsWithdrawalPanel);
        currentScreen.setVisible(true);
    }

    /**
     * Sets account selection panel as the active screen
     */
    public void setAccSelPanel()
    {
        currentScreen = atmsAccSelPanel;
        atmsAccSelPanel.clear();
        atmsAccSelPanel.showAccounts();
        add(atmsAccSelPanel, BorderLayout.CENTER);
        keypadPanel.setCurrentScreenPanel(atmsAccSelPanel);
        currentScreen.setVisible(true);
    }

     /**
     * Sets target account selection panel as the active screen
     */
    public void setTgtAccSelPanel()
    {
        currentScreen = atmsTgtAccSelPanel;
        atmsTgtAccSelPanel.clear();
        atmsTgtAccSelPanel.showAccounts();
        add(atmsTgtAccSelPanel, BorderLayout.CENTER);
        keypadPanel.setCurrentScreenPanel(atmsTgtAccSelPanel);
        currentScreen.setVisible(true);
    }

    /**
    * Sets log in panel
    */
    public void setLogInPanel()
    {
        logInPanel.clearInputFields();
        currentScreen = logInPanel;
        add(logInPanel, BorderLayout.CENTER);
        keypadPanel.setCurrentScreenPanel(logInPanel);
        currentScreen.setVisible(true);

        // ensure envelope slot has been reset
        envelopeSlot.setText("ENVELOPE SLOT");
        envelopeSlot.setEnabled(false);
    }

    /**
    * Sets another transaction prompt panel
    */
    public void setAnotherTransPanel(String header)
    {
        atmsAnotherTransPanel.clearHeader();
        currentScreen = atmsAnotherTransPanel;
        atmsAnotherTransPanel.setHeader(header);
        add(atmsAnotherTransPanel, BorderLayout.CENTER);
        keypadPanel.setCurrentScreenPanel(atmsAnotherTransPanel);
        currentScreen.setVisible(true);
    }

    /**
    * Sets another account information panel
    */
    public void setAccInfoMenuPanel()
    {
        currentScreen = atmsAccInfoMenuPanel;
        add(atmsAccInfoMenuPanel, BorderLayout.CENTER);
        keypadPanel.setCurrentScreenPanel(atmsAccInfoMenuPanel);
        currentScreen.setVisible(true);
    }

    public void setAmtInputPanel()
    {
        atmsAmtInputPanel.clearInput();
        currentScreen = atmsAmtInputPanel;
        add(atmsAmtInputPanel, BorderLayout.CENTER);
        keypadPanel.setCurrentScreenPanel(atmsAmtInputPanel);
        atmsAmtInputPanel.setVisible(true);
    }

    public void setAccInfoPanel()
    {
        atmsAccInfoPanel.clear();
        atmsAccInfoPanel.populate();
        currentScreen = atmsAccInfoPanel;
        add(atmsAccInfoPanel, BorderLayout.CENTER);
        keypadPanel.setCurrentScreenPanel(atmsAccInfoPanel);
        atmsAccInfoPanel.setVisible(true);
    }

    public void showReceipt(String receipt)
    {
        Receipt test = new Receipt(receipt, this);
    }

    public void showDispenseCash(String cash)
    {
        cashDispenser.setText("$" + cash + " DISPENSED");
        cashDispenser.setEnabled(true);
    }

    public void showEnvelopeSlot()
    {
        envelopeSlot.setText("INSERT ENVELOPE");
        envelopeSlot.setEnabled(true);
    }

    public boolean isCurrScreenVisible()
    {
        return currentScreen.isVisible();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}